from datetime import datetime

from api_utils.voice_api import VoiceApi
from database.models.asin_model import AsinData
from database.models.store_model import StoreData
from database.models.user_model import UserData
from operation.asin_operation import AsinOperation
from operation.store_operation import StoreOperation
from operation.user_operation import UserOperation


def main():
    Monitor.monitor_store()
    Monitor.monitor_user()
    Monitor.monitor_asin()


class Monitor:

    @staticmethod
    def monitor_store():
        old_sid_list = StoreData.get_sid_list()
        new_sid_list = StoreOperation.get_sid_list()
        print("旧店铺id列表为：{}".format(old_sid_list))
        print("新店铺id列表为：{}".format(new_sid_list))
        if old_sid_list == new_sid_list:
            print("店铺信息未发生变化")
        del_sid_list = list(set(old_sid_list) - set(new_sid_list))
        new_sid_list = list(set(new_sid_list) - set(old_sid_list))
        print("删除的id列表为：{}".format(old_sid_list))
        print("新增的店铺id列表为：{}".format(new_sid_list))
        if len(del_sid_list) > 0:
            print("有店铺删除")
        if len(new_sid_list) > 0:
            print("有新增店铺")
            for sid in new_sid_list:
                principal_name = AsinData.get_principal_info_by_sid(sid)
                mobile = UserData.get_mobile_by_username(principal_name)
                store_name = StoreData.get_store_name_by_sid("10896")
                contents = ("赵老板", store_name, "", "", "")
                VoiceApi.call_user(mobile, 5, contents)
    StoreData.del_store_info()
    StoreOperation.handle_store_info()

    @staticmethod
    def monitor_user():
        old_uid_list = UserData.get_user_id_list()
        new_uid_list = UserOperation.get_user_id_list()
        print("旧用户id列表为：{}".format(old_uid_list))
        print("新用户id列表为：{}".format(new_uid_list))
        if old_uid_list == new_uid_list:
            print("用户信息未发生变化")
        del_uid_list = list(set(old_uid_list) - set(new_uid_list))
        new_uid_list = list(set(old_uid_list) - set(new_uid_list))
        print("删除的用户id列表为：{}".format(del_uid_list))
        print("新增的用户id列表为：{}".format(new_uid_list))
        if len(del_uid_list) > 0:
            print("有用户删除")
            for uid in del_uid_list:
                mobile = UserData.get_mobile_by_uid(uid)
                user_name = UserData.get_username_by_uid(uid)
                contents = ("赵老板", user_name, "123", "", "")
                VoiceApi.call_user(mobile, 1,contents)
        if len(new_uid_list) > 0:
            print("有新增用户")
            for uid in new_uid_list:
                mobile = UserData.get_mobile_by_uid(uid)
                user_name = UserData.get_username_by_uid(uid)
                contents = ("赵老板", user_name, uid, "", "")
                VoiceApi.call_user(mobile, 2, contents)
        UserData.delete_user_info()
        UserOperation.handle_user_info()

    @staticmethod
    def monitor_asin():
        max_date = AsinData.get_asin_max_date()
        today = datetime.today().strftime("%y%m%d")
        if max_date != today:
            AsinOperation.handle_asin_info()


if __name__ == '__main__':
    main()